import 'package:todo_app/controllers/definitions/Definition.dart';
/// 本地待发送消息
class RTCMessage extends Definition {
  RTCMsgType? type;
  dynamic payload;
  int? suid;
  String? room;
  int? timestamp;

  RTCMessage({ this.type, this.payload, this.suid, this.room, this.timestamp });
  
  @override
  void fromJson(Map<String, dynamic> json) {
    type = RTCMsgType.fromName(json["type"]);
    payload = json["payload"];
    suid = json["suid"];
    room = json["room"];
    timestamp = json["timestamp"];
  }
  
  @override
  Map<String, dynamic> toJson() {
    return {
      "room": room,
      "suid": suid,
      "payload": payload,
      "type": type?.name,
      "timestamp": timestamp,
    };
  }
}


/// 发送消息类型
enum RTCMsgType {
  joinRoom("joinRoom"), // 加入房间
  exitRoom("exitRoom"), // 退出房间
  offer("offer"), // 传递offer
  answer("answer"), // 传递answer
  candidate("candidate"), // 传递candidate
  reject("reject"), // 拒绝接听
  accept("accept"), // 接听
  handUp("handUp"), // 挂断通话
  invite("invite") // 邀请通话
;

  const RTCMsgType(this.name);
  final String name;

  static RTCMsgType? fromName(String name) {
    switch (name) {
      case 'joinRoom': return RTCMsgType.joinRoom;
      case 'exitRoom': return RTCMsgType.exitRoom;
      case 'offer': return RTCMsgType.offer;
      case 'answer': return RTCMsgType.answer;
      case 'candidate': return RTCMsgType.candidate;
      case 'reject': return RTCMsgType.reject;
      case 'accept': return RTCMsgType.accept;
      case 'handUp': return RTCMsgType.handUp;
      case 'invite': return RTCMsgType.invite;
      default: return null;
    }
  }
}

class CandidateMsgPayload extends Definition {
  String? sdp;
  String? sdpMid;
  String? sdpMLineIndex;
  
  @override
  void fromJson(Map<String, dynamic> json) {
    sdp = json["sdp"];
    sdpMid = json["sdpMid"];
    sdpMLineIndex = json["sdpMLineIndex"];
  }
  
  @override
  Map<String, dynamic> toJson() {
    return {
      "sdp": sdp,
      "sdpMid": sdpMid,
      "sdpMLineIndex": sdpMLineIndex
    };
  }
}